<template>
    <!--上传题库 隐藏-->
    <n-upload
        v-show="false"
        :action="app.ip +'super_admin/question'"
        method="put"
        :headers="{
        token: user.getToken
      }"
        :data="{
        project_id: checkId
      }"
        @finish="handleFinish"
    >
        <span ref="refUpQuestionBank">上传题库</span>
    </n-upload>
    <!--上传数据源-->

    <UploadDataSourceModal ref="uploadDataSourceModalRef" :key="UploadDataSourceModalKey" :set-key="setKey"
                           v-show="props.currentPage==='dataSource'"/>
    <n-data-table
        :columns="columns[props.currentPage]"
        :data="superAdmin[props.currentPage].enable"
        children-key="noChildren"
        :max-height="'70vh'"
    />
    <n-modal v-model:show="questionBankShowModal">
        <n-card
            style="width: 600px"
            :title="questionBank.name"
            :bordered="false"
            size="huge"
            role="dialog"
            aria-modal="true"
        >
            <n-scrollbar style="height: 60vh" trigger="none">
                <div class="questionBank" v-for="(index,item) in questionBank.children">
                    <p>
                        <span>{{ item + 1 }} : </span>
                        <span>{{ index.question }}</span>
                        <span style="font-size: 16px">  答案: {{ index.answer }}</span>
                    </p>
                    <p>
                        <span>A: </span>
                        <span>{{ index.A }}</span>
                    </p>
                    <p>
                        <span>B: </span>
                        <span>{{ index.B }}</span>
                    </p>
                    <p>
                        <span>C: </span>
                        <span>{{ index.C }}</span>
                    </p>
                    <p>
                        <span>D: </span>
                        <span>{{ index.D }}</span>
                    </p>
                </div>

            </n-scrollbar>
        </n-card>
    </n-modal>
</template>

<script setup>
import {useSuperAdminStore} from "@/stores/superAdmin";
import {useUserStore} from "@/stores/user";
import {useAppStore} from "@/stores/app";
import {columns} from "@/components/superAdmin/资源管理/columns";
import workApi from "@/http/workApi";
import UploadDataSourceModal from "@/components/superAdmin/新建模态框/UploadDataSourceModal.vue";
import {useDialog} from "naive-ui";

const superAdmin = useSuperAdminStore()
const user = useUserStore()
const app = useAppStore()
const refUpQuestionBank = ref(null)
const props = defineProps(['currentPage'])
const uploadDataSourceModalRef = ref(null)
const dialog = useDialog()
// 操作
const UploadDataSourceModalKey = ref(0)
const setKey = () => {
    UploadDataSourceModalKey.value++
}
const modify = (row) => {
    uploadDataSourceModalRef.value.setDataSource(row)
}
const questionBankShowModal = ref(false)
const questionBank = ref(null)
const view = (row) => {
    if ('type' in row && row.type === '项目') {
        console.log(row)
        workApi.post("super_admin/question", {project_id: row.id}).then(r => {
            console.log(r.data[0])
            questionBank.value = r.data[0]
            questionBank.value.projectName = row.name
            console.log(questionBank.value)
            questionBankShowModal.value = true
        })
    } else {
        workApi.post('super_admin/get_data_source', {
            id: [row.id]
        }).then((res) => {
            console.log(res)
            if ('web_url' in res.data) {
                window.open(res.data['web_url'], '_blank')
                return
            }
            window.open(res.data['url'], '_blank');
        })
    }
    window.$message.info('@查看 ' + row.name);
}
let checkId = ref(null)
const upQuestionBank = (row) => { // 上传题库
    checkId.value = row.id
    refUpQuestionBank.value.click()
}
const del = (row) => {
    dialog.warning({
        title: '删除',
        content: '你确定？',
        positiveText: '确定',
        negativeText: '不确定',
        onPositiveClick: () => {
            workApi.delete("super_admin/data_source", {
                data: {
                    id: row.id
                }
            }).then(() => {
                superAdmin.updateDataSource()
            })
        },
    })

};
// 操作集合
const operation = {modify, view, upQuestionBank, del}
columns.operation = operation

// 上传题库后的回调函数
let handleFinish = ({event}) => {
    // 获取返回内容
    let res = eval('[' + event.target.response + ']')[0]
    window.$message.info(res['tip'].info)
    if (res.success) {
        superAdmin.updateTaskQuestionBank()
    }
}

</script>

<style scoped lang="less">
.questionBank {
    margin-bottom: 30px;
    p:nth-child(n+2) {
        padding-left: 15px;
    }
}
</style>